package com.example.lotterysystem.service.operater;

import com.example.lotterysystem.dao.dataobject.ActivityPrizeDO;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2025/2/3
 */
@Component
public class PrizeOperator extends AbstractActivityOperator {
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        if (activityId == null
                || prizeId == null
                || targetPrizeStatus == null) {
            return false;
        }
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectPrizeByAPId(activityId, prizeId);
        if (activityPrizeDO == null) {
            return false;
        }
        //判断状态
        if (activityPrizeDO.getStatus().equalsIgnoreCase(targetPrizeStatus.name())) {
            return false;
        }
        return true;
    }

    @Override
    public boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        try {
            activityPrizeMapper.updateStatus(activityId, prizeId, targetPrizeStatus.name());
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
